﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;

using AST.Model;
using AST.DAL;
using AST.Dapper;
using AST.Common;

namespace AST.BLL
{
    public class AdminHelper
    {
        public static bool Login(string userName, string userPsw, string ip, out AdminEntity objUser)
        {
            objUser = null;
            StringBuilder sb = new StringBuilder();
            sb.Append("UPDATE tblAdministrator SET LastLogonIP = @LastLogonIP, LastLogonTime = CURDATE() WHERE UserName = @UserName AND UserPassword = @UserPsw AND ActiveFlg = 1 ;");
            sb.Append("SELECT * FROM tblAdministrator WHERE UserName = @UserName AND UserPassword = @UserPsw AND ActiveFlg = 1 ;");
            try
            {
                var param = new DynamicParameters();
                param.Add("@UserName", userName);
                param.Add("@UserPsw", userPsw);
                param.Add("@LastLogonIP", ip);

                objUser = DBHelper.ReadObject<AdminEntity>(sb.ToString(), param);

                if (objUser == null)
                {
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                Logging.Logging.RecordError(ex);
                return false;
            }
        }

        public static bool InserAdmin(string UserName, string Password)
        {
            AdminEntity obj = new AdminEntity();
            obj.UserName = UserName;
            obj.UserPassword = Password;
            obj.IsSystemUser = 1;
            return AddUser(obj);
        }

        public static bool AddUser(AdminEntity obj)
        {
            if (CheckUserIsExist(obj))
                return false;
            try
            {
                var param = new DynamicParameters();
                param.Add("@UserName", obj.UserName);
                param.Add("@UserPassword", obj.UserPassword);
                param.Add("@IsSystemUser", obj.IsSystemUser);

                string sql = "insert tblAdministrator(UserName,UserPassword,IsSystemUser,CreateTime,ActiveFlg) values(@UserName,@UserPassword,@IsSystemUser,CURRENT_TIMESTAMP(),1)";

                DBHelper.ExcuteSQL(sql, param);
            }
            catch (Exception ex)
            {
                Logging.Logging.RecordError(ex);
                return false;
            }
            return true;
        }

        public static bool UpdateUser(AdminEntity obj)
        {
            if (CheckUserIsExist(obj))
                return false;
            try
            {
                string sql = "UPDATE tblAdministrator SET UserName=@UserName,UserPassword=@UserPassword WHERE UserID=@UserID";
                var param = new DynamicParameters();
                param.Add("@UserID", obj.UserID);
                param.Add("@UserName", obj.UserName);
                param.Add("@UserPassword", obj.UserPassword);

                DBHelper.ExcuteSQL(sql, param);
            }
            catch (Exception ex)
            {
                Logging.Logging.RecordError(ex);
                return false;
            }
            return true;
        }

        public static bool CheckUserIsExist(AdminEntity obj)
        {
            try
            {
                string sql = "select count(*) from tblAdministrator where UserName = @UserName and ActiveFlg = 1 and UserID <> @UserID ";
                var param = new DynamicParameters();
                param.Add("@UserID", obj.UserID);
                param.Add("@UserName", obj.UserName);
                int count = DBHelper.ExecuteScalar(sql, param);
                return (count > 0) ? true : false;
            }
            catch (Exception ex)
            {
                Logging.Logging.RecordError(ex);
                return false;
            }
        }
        public static bool ChangePassword(AdminEntity obj)
        {
            try
            {
                string sql = "UPDATE tblAdministrator SET UserPassword=@UserPassword WHERE UserID=@UserID";
                var param = new DynamicParameters();
                param.Add("@UserID", obj.UserID);
                param.Add("@UserPassword", obj.UserPassword);

                DBHelper.ExcuteSQL(sql, param);
            }
            catch (Exception ex)
            {
                Logging.Logging.RecordError(ex);
                return false;
            }
            return true;
        }
        public static bool DeleteUser(int userid)
        {
            try
            {
                string sql = "UPDATE tblAdministrator SET ActiveFlg=0 WHERE UserID=@UserID";
                var param = new DynamicParameters();
                param.Add("@UserID", userid);

                DBHelper.ExcuteSQL(sql, param);
            }
            catch (Exception ex)
            {
                Logging.Logging.RecordError(ex);
                return false;
            }
            return true;
        }

        public static List<AdminEntity> Load()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select * from tblAdministrator where ActiveFlg = 1 and IsSystemUser = 0 ");
            sb.Append("order by CreateTime Desc ");

            List<AdminEntity> objlist = null;
            try
            {
                objlist = DBHelper.ReadCollection<AdminEntity>(sb.ToString(), null);
            }
            catch (Exception ex)
            {
                Logging.Logging.RecordError(ex);
            }
            if (objlist == null)
                objlist = new List<AdminEntity>();
            return objlist;
        }
    }
}
